<?php
require_once 'Constants.php';

/**
 * discovers if haystack starts with needle
 *
 * @param string $needle
 * @param string $haystack
 * @return boolean
 */
function StartsWith($needle, $haystack) {
	return (strtolower(substr($haystack,0,strlen($needle))) == strtolower($needle));
}

/**
 * discovers if haystack ends with needle
 *
 * @param string $needle
 * @param string $haystack
 * @return boolean
 */
function EndsWith($needle, $haystack) {
	// Get the length of the end string
	$StrLen = strlen ( $needle );
	// Look at the end of FullStr for the substring the size of EndStr
	$FullStrEnd = substr ( $haystack, strlen ( $haystack ) - $StrLen );
	// If it matches, it does end with EndStr
	return $FullStrEnd == $needle;
}

/**
 * Gets the type of the field property depending on it's index
 *
 * @param integer $key
 * @return string the field type
 */
function GetFieldPropertyType($key) {
	if ($key == 0) {
		return FIELD_NAME_FIELD;
	} else if ($key == 1) {
		return DATA_TYPE_FIELD;
	} else if ($key == 2) {
		return COLLATION_FIELD;
	} else if ($key == 3) {
		return NULLABLE_FIELD;
	} else if ($key == 4) {
		return INDEX_FIELD;
	} else if ($key == 5) {
		return DEFAULT_VALUE_FIELD;
	} else if ($key == 6) {
		return SPECIAL_FIELD;
	} else if ($key == 7) {
		return PERMISSIONS_FIELD;
	} else if ($key == 8) {
		return COMMENTS_FIELD;
	} else {
		return $key;
	}
}

/**
 * Takes the mysql data type name and returns the appropriate PHP type name
 * MySQL: (int, tinyint, smallint, mediumint, bigint), float, double, (real, decimal, numeric), varchar, binary, (timestamp, DateTime)
 * PHP: int, bool, float, double, real, string, binary, DateTime
 *
 * @param string $mysql_type_name
 * @return string the php type name
 */
function ConvertMySQLTypeNameToPHPTypeName($mysql_type_name) {
	if (StartsWith ( "int", $mysql_type_name ) || StartsWith ( "smallint", $mysql_type_name ) || StartsWith ( "mediumint", $mysql_type_name ) || StartsWith ( "bigint", $mysql_type_name )) {
		return "integer";
	} else if (StartsWith ( "tinyint", $mysql_type_name )) {
		return "boolean";
	} else if (StartsWith ( "float", $mysql_type_name )) {
		return "float";
	} else if (StartsWith ( "double", $mysql_type_name )) {
		return "double";
	} else if (StartsWith ( "real", $mysql_type_name ) || StartsWith ( "decimal", $mysql_type_name ) || StartsWith ( "numeric", $mysql_type_name )) {
		return "real";
	} else if (StartsWith ( "varchar", $mysql_type_name ) || StartsWith ( "char", $mysql_type_name )) {
		return "string";
	} else if (StartsWith ( "text", $mysql_type_name ) || StartsWith ( "longtext", $mysql_type_name )) {
		return "TextArea";
	} else if (StartsWith ( "blob", $mysql_type_name )) {
		return "binary";
	} else if (StartsWith ( "time", $mysql_type_name ) || StartsWith ( "DateTime", $mysql_type_name ) || StartsWith ( "Year", $mysql_type_name )) {
		return "DateTime";
	} else if (StartsWith ( "Date", $mysql_type_name )) {
		return "Date";
	}
	
	print ( "UNKNOWN MySQL Data Type: $mysql_type_name <br>" );
	debug_print_backtrace();
}

/**
 * Writes a string to a file
 *
 * @param string $filename
 * @param string $string
 * @return int number of bytes written
 */
function WriteToFile($filename, $string) {
	$fp = fopen ( $filename, 'w' );
	$written = 0;
	while ( $written < strlen ( $string ) ) {
		$fwrite = fwrite ( $fp, substr ( $string, $written ) );
		if (! $fwrite) {
			return $fwrite;
		}
		$written += $fwrite;
	}
	return $written;
}

/**
 * Copies the file if it does not yet exist
 *
 * @param string $old_file_path
 * @param string $new_file_path
 * @return Copy success (true if file did not exist)
 */
function conditionalCopyFile($old_file_path, $new_file_path) {
	if (! is_file ( $new_file_path )) {
		if (copy ( $old_file_path, $new_file_path )) {
			return TRUE;
		}
	}
	return FALSE;
}

/**
 * Creates the directory if it does not exist
 *
 * @param string $dir_path
 * @return boolean Directory created or not
 */
function conditionalCreateDirectory($dir_path) {
	if (! is_dir ( $dir_path )) {
		mkdir ( $dir_path, 0755, false );
		return TRUE;
	} else {
		return FALSE;
	}
}

/**
 * Copies a directory and it's sub directories and files
 * @param string $src
 * @param string $dst
 */
function recursiveCopy($src, $dst) {
	$dir = opendir ( $src );
	@mkdir ( $dst );
	while ( false !== ($file = readdir ( $dir )) ) {
		if (($file != '.') && ($file != '..') && ($file != '.svn')) {
			if (is_dir ( $src . '/' . $file )) {
				recursiveCopy ( $src . '/' . $file, $dst . '/' . $file );
			} else {
				copy ( $src . '/' . $file, $dst . '/' . $file );
			}
		}
	}
	closedir ( $dir );
}

/**
 * Formats the field_name into title Camel Case (with spaces)
 * @param string $field_name
 * @return string The formatted field name
 */
function formatFieldNameToTitle($field_name) {
	//replace underscores with spaces
	$formattedFieldName = str_ireplace ( "_", " ", $field_name );
	//upper case the first letter of each word
	$formattedFieldName = ucwords ( $formattedFieldName );
	
	return $formattedFieldName;
}

/**
 * Formats the field_name into readable name (with spaces)
 * @param string $field_name
 * @return string The formatted field name
 */
function formatFieldNameToReadable($field_name) {
	//replace underscores with spaces
	$formattedFieldName = str_ireplace ( "_", " ", $field_name );
	
	return $formattedFieldName;
}

/**
 * Gets the properties of the class required for creating a new one of the class
 * Ignores db element properties and array properties
 * @param string $class DBElement class
 * @return string
 */
function getClassProperties($class) {
	$db_element_vars = get_class_vars ( "DBElement" );
	$db_element_var_keys = array_keys ( $db_element_vars );
	$class_instance = new $class ( );
	$class_vars = get_class_vars ( $class );
	$props_list = "";
	foreach ( $class_vars as $prop_name => $prop_val ) {
		if (! in_array ( $prop_name, $db_element_var_keys ) && $prop_name != $class_instance->primary_key_field && ! is_array ( $prop_val )) {
			$props_list .= $prop_name . ", ";
		}
	}
	
	return substr ( $props_list, 0, strlen ( $props_list ) - 2 );
}

?>